//
//  WXPBannerView.m
//  MexicoLoan
//
//  Created by wxp on 2025/4/14.
//
//

#import "WXPBannerView.h"
#import "WXPBannerWebVC.h"

@interface WXPBannerView ()

@end

@implementation WXPBannerView

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, 0);
        self.backgroundColor = [UIColor clearColor];
        [self addSubView];
    }
    return self;
}

- (void)addSubView
{
    [self addSubview:self.boxView];
    [self.boxView addSubview:self.autoScView];
}

#pragma mark - lazy
- (UIButton *)boxView
{
    if (_boxView == nil) {
        _boxView = [UIButton  new];
        [GlobalMethod setRoundView:_boxView color:COLOR_LINE numRound:(9) width:1];
        _boxView.width = SCREEN_WIDTH-2*LEFT_MARDIN;
    }
    return  _boxView;
}
- (WXPBannerAutoScView *)autoScView
{
    if (_autoScView == nil) {
        _autoScView = [WXPBannerAutoScView new];
        _autoScView.isClickValid = YES;
        _autoScView.isShowProductImageStyle = YES;
        _autoScView.pageControlToBottom = (16);
        _autoScView.pageCurrentColor = COLOR_PRIMARY;
        _autoScView.pageDefaultColor = [UIColor colorWithHexString:@"#141414" alpha:0.4];
        [_autoScView timerStart];
        _autoScView.backgroundColor = COLOR_WHITE;
        WEAKSELF
        _autoScView.blockNum = ^(int num) {
            NSLog(@"轮播图点击 ------ %d",num);
            ModelBanner * model = weakSelf.bannerList[num];
            // 0：内部webView 1：外部浏览器
            if (model.jumpType == 0) {
                // 内部webView
                WXPBannerWebVC * vc = [WXPBannerWebVC new];
                vc.htmlUrl = model.jumpUrl;
                [Global_Nav pushViewController: vc animated:true];
            } else if (model.jumpType == 1) {
                // 外部浏览器
                NSURL *url = [NSURL URLWithString:model.jumpUrl];
                // 确保 URL 有效
                if (url) {
                    // 使用 UIApplication 打开 URL
                    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
                        if (success) {
                            NSLog(@"URL 打开成功");
                        } else {
                            NSLog(@"无法打开 URL");
                        }
                    }];
                } else {
                    // 处理无效的 URL
                    NSLog(@"无效的 URL");
                }
            }
        };
    }
    return _autoScView;
}

#pragma mark - 刷新View
- (void)resetViewWithAry:(NSMutableArray *)ary{
    self.bannerList = ary;
    
    self.boxView.width = SCREEN_WIDTH-2*LEFT_MARDIN;
    self.boxView.height = self.boxView.width/3;
    self.boxView.centerXTop = XY(SCREEN_WIDTH/2, 0);
    
    self.autoScView.widthHeight = self.boxView.widthHeight;
    [GlobalMethod setRoundView:self.autoScView color:[UIColor clearColor] numRound:(7) width:0];
    self.autoScView.centerXCenterY = XY(self.boxView.width/2, self.boxView.height/2);
    
    
    NSMutableArray * imgUrlAry = [NSMutableArray new];
    for (ModelBanner * model in ary) {
        NSString * imgUrl = [NSString stringWithFormat:@"%@", model.bannerUrl];
        [imgUrlAry addObject:imgUrl];
    }
    [self.autoScView resetWithImageAry:imgUrlAry];
    self.autoScView.pageControlToBottom = (0);
    
    self.height = self.boxView.bottom+(20);
}

@end
